<template>
  <el-container>
    <el-header>
      <div class="left-box">
        <el-popconfirm
          title="确认关闭？如果有内容变更，请先保存"
          confirm-button-text="确认"
          cancel-button-text="取消"
          @confirm="$router.push('/article')"
        >
          <template #reference>
            <el-button>x</el-button>
          </template>
        </el-popconfirm>

        <el-input v-model="title" placeholder="请输入文章标题" />
      </div>

      <div class="right-box">
        <el-button type="primary" @click="issue">发布</el-button>
        <el-dropdown>
          <div class="set">· · ·</div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item> 查看 </el-dropdown-item>

              <el-dropdown-item @click="visible = true">
                设置
              </el-dropdown-item>

              <el-dropdown-item> 保存草稿 </el-dropdown-item>

              <el-dropdown-item> 删除 </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <el-main>
      <!-- 富文本 -->
      <v-md-editor v-model="txt" height="400px"></v-md-editor>

      <!-- 抽屉 -->
      <el-drawer v-model="visible" direction="rtl">
        <template #title>
          <h4>文章设置</h4>
        </template>

        <template #default>
          <div>
            <el-form :model="form" label-width="120px">
              <el-form-item label="文章摘要">
                <el-input
                  v-model="form.digest"
                  type="textarea"
                  placeholder="请输入文章摘要"
                />
              </el-form-item>

              <el-form-item label="访问密码">
                <el-input
                  v-model="form.password"
                  placeholder="输入后查看需要密码"
                />
              </el-form-item>

              <el-form-item label="付费查看">
                <el-input v-model="form.pay" placeholder="输入后需要支付费用" />
              </el-form-item>

              <el-form-item label="开启评论">
                <el-switch v-model="form.review" />
              </el-form-item>

              <el-form-item label="首页推荐">
                <el-switch v-model="form.recommend" />
              </el-form-item>

              <el-form-item label="选择分类">
                <el-select v-model="form.classify" placeholder="请选择分类">
                  <el-option
                    v-for="item in classify"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>

              <el-form-item label="选择标签">
                <el-select v-model="form.tag" placeholder="请选择标签">
                  <el-option
                    v-for="item in tag"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-form>
          </div>
        </template>
        <template #footer>
          <div style="flex: auto">
            <el-button type="primary" @click="confirmClick">确认</el-button>
          </div>
        </template>
      </el-drawer>
    </el-main>
  </el-container>
</template>

<script>
import { ElMessage } from "element-plus";
import { apiAddArticle } from "@/api/article";
import { apiClassify } from "@/api/classify";
import { apiTags } from "@/api/tags";

export default {
  data() {
    return {
      txt: "",
      ws: null,
      title: "",
      visible: false,
      form: {},
      classify: [],
      tag: [],
    };
  },
  setup() {
    return {
      props: {
        // props.
        multiple: true,
      },
    };
  },
  methods: {
    issue() {
      if (this.title === "") {
        ElMessage.warning("请输入文章标题");
        return;
      } else if (this.txt === "") {
        ElMessage.warning("请输入文章内容");
        return;
      } else {
        apiAddArticle({ title: this.title, txt: this.txt, ...this.form }).then(
          (res) => {
            ElMessage.success(res.msg);
          }
        );
      }
    },
    confirmClick() {
      this.visible = false;
    },
  },
  mounted() {
    apiClassify().then((res) => {
      this.classify = res.data;
    });
    apiTags().then((res) => {
      this.tag = res.data;
    });
  },
};
</script>

<style lang="scss" scoped>
.el-header {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.left-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.right-box {
  width: 100px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.set {
  text-align: center;
  cursor: pointer;
}
</style>
